package com.nulldev.util.web.YouTubeExtractor.demo;

import java.util.List;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.JVM;
import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ExecutorManager;
import com.nulldev.util.concurrency.threadIt.v4.emapi.uft.Executable;
import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.io.FastIO.FastIO;
import com.nulldev.util.web.YouTubeExtractor.YouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractorConstants;
import com.nulldev.util.web.YouTubeExtractor.extractors.util.YTIAPIClientType;
import com.nulldev.util.web.YouTubeExtractor.items.YouTubeVideoHeatmap;
import com.nulldev.util.web.YouTubeExtractor.video.ExtractedVideo;
import com.nulldev.util.web.YouTubeExtractor.video.YouTubeVideo;
import com.nulldev.util.web.legacyURI.LegacyURIUtils;

public class RewriteDemoABI2 {

	public static void main(String[] args) {
		Arguments.setup(args);
		LegacyURIUtils.disableSSLVerification();
		final IYouTubeExtractor ex = YouTubeExtractor.get("ytiapi.native-rolling");
		ex.enableInstanceDebugging(true);
		final Executable<ExtractedVideo> f = ex.generateTask(FastMaps.fixedUniMap(new Object[][]
			{
					{ IYouTubeExtractorConstants.MAP_TYPE_VIDEO_ID, "b7V92lQMaH8" },
					{ IYouTubeExtractorConstants.MAP_TYPE_ENABLE_DEBUG_API_RESPONSE_PRINT, true },
//					{ IYouTubeExtractorConstants.MAP_TYPE_LIVESTREAMS_KEEP_RAW_STREAMS, true },
					{ IYouTubeExtractorConstants.MAP_TYPE_ENABLE_ADVERTISING_METADATA_EXTRACTION, true },
					{ IYouTubeExtractorConstants.MAP_TYPE_USE_CLIENT_TYPE, YTIAPIClientType.ANDROID }
					/* */
			}));
		ExecutorManager.get().asyncSubmit(f);
		final long s0 = System.currentTimeMillis();
		JVM.println("result: " + f.waitTillCompletion());
		JVM.println("sync: " + f);
		final long e0 = System.currentTimeMillis();
		JVM.println("Execution Time: " + (e0 - s0) + " ms");
		if (f.result() == null) {
			throw new NullPointerException("Failed to get a result!");
		}
		if (f.result().supportsLateExtraction()) {
			f.result().awaitCompletion();
			JVM.println("lateExtraction: COMPLETE");
		}
		if (!f.result().getResult().successful()) {
			JVM.println("Failed to extract.");
			FastIO.get().flush();
			JVM.exit();
			return;
		}
		if (f.result().hasAdvertisingMetadata()) {
			JVM.println("advertising: ");
			ArrayUtils.printLln(f.result().getAdvertisingMetadata().getAdPlacements());
		}
		if (f.result().hasCommentData()) {
			JVM.println("commentData: " + f.result().getCommentData());
		}
		if (f.result().hasChatPayload()) {
			JVM.println("chatPayload: " + f.result().getChatPayload());
		}
		if (f.result().getMetadata().hasHeatmap()) {
			final Executable<YouTubeVideoHeatmap> exh = f.result().getMetadata().getHeatmap();
			JVM.println("heatmap: " + exh);
			if (exh.hasFailed()) {
				JVM.errorln(exh.getException());
			}
		}
		if (f.result().getMetadata().hasChapters()) {
			JVM.println("chapters: " + f.result().getMetadata().getChapters());
		}
		final long s = System.currentTimeMillis();
		if (f.result().getMetadata() != null)
			JVM.println("enhanced description: " + f.result().getMetadata().getEnhancedDescription());
		final long e = System.currentTimeMillis();
		JVM.println("lat: " + (e - s) + " ms");
		if (f.result() != null) {
			final List<YouTubeVideo> result = f.waitTillCompletion().videos();
			result.forEach((video) -> {
				final String url = video.videoURL();
				JVM.println("fmt: " + video.getFormat() + ", " + url);
			});
			JVM.println(f.result().getMetadata());
		} else {
			JVM.println("f.result() was null.");
		}
		FastIO.get().flush();
		JVM.exit();
	}
}
